package com.jxw.launcher.util;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/* loaded from: classes.dex */
public class FileUtils {
    private static final int FAILED = 0;
    private static final int SUCCESS = 1;
    private static FileUtils instance;
    private FileOperateCallback callback;
    private Context context;
    private String errorStr;
    private Handler handler = new Handler(Looper.getMainLooper()) { // from class: com.jxw.launcher.util.FileUtils.1
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (FileUtils.this.callback != null) {
                if (message.what == 1) {
                    FileUtils.this.callback.onSuccess();
                }
                if (message.what == 0) {
                    FileUtils.this.callback.onFailed(message.obj.toString());
                }
            }
        }
    };
    private volatile boolean isSuccess;

    /* loaded from: classes.dex */
    public interface FileOperateCallback {
        void onFailed(String str);

        void onSuccess();
    }

    public static FileUtils getInstance(Context context) {
        if (instance == null) {
            instance = new FileUtils(context);
        }
        return instance;
    }

    private FileUtils(Context context) {
        this.context = context;
    }

    public FileUtils copyAssetsToSD(final String str, final String str2) {
        new Thread(new Runnable() { // from class: com.jxw.launcher.util.FileUtils.2
            @Override // java.lang.Runnable
            public void run() {
                FileUtils fileUtils = FileUtils.this;
                fileUtils.copyAssetsToDst(fileUtils.context, str, str2);
                if (FileUtils.this.isSuccess) {
                    FileUtils.this.handler.obtainMessage(1).sendToTarget();
                } else {
                    FileUtils.this.handler.obtainMessage(0, FileUtils.this.errorStr).sendToTarget();
                }
            }
        }).start();
        return this;
    }

    public void setFileOperateCallback(FileOperateCallback fileOperateCallback) {
        this.callback = fileOperateCallback;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void copyAssetsToDst(Context context, String str, String str2) {
        try {
            String[] list = context.getAssets().list(str);
            if (list.length > 0) {
                File file = new File(Environment.getExternalStorageDirectory(), str2);
                if (!file.exists()) {
                    file.mkdirs();
                }
                for (String str3 : list) {
                    if (str.equals("")) {
                        copyAssetsToDst(context, str3, str2 + File.separator + str3);
                    } else {
                        copyAssetsToDst(context, str + File.separator + str3, str2 + File.separator + str3);
                    }
                }
            } else {
                File file2 = new File(Environment.getExternalStorageDirectory(), str2);
                InputStream open = context.getAssets().open(str);
                FileOutputStream fileOutputStream = new FileOutputStream(file2);
                byte[] bArr = new byte[1024];
                while (true) {
                    int read = open.read(bArr);
                    if (read == -1) {
                        break;
                    }
                    fileOutputStream.write(bArr, 0, read);
                }
                fileOutputStream.flush();
                open.close();
                fileOutputStream.close();
            }
            this.isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            this.errorStr = e.getMessage();
            this.isSuccess = false;
        }
    }

    public static String getFileContent(File file) {
        String str = "";
        if (!file.isDirectory() && file.getName().endsWith("txt")) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
                while (true) {
                    String readLine = bufferedReader.readLine();
                    if (readLine == null) {
                        break;
                    }
                    str = str + readLine + "\n";
                }
                fileInputStream.close();
            } catch (FileNotFoundException unused) {
                Log.d("TestFile", "The File doesn't not exist.");
            } catch (IOException e) {
                Log.d("TestFile", e.getMessage());
            }
        }
        return str;
    }

    public static String getBatteryValue() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("/sys/class/power_supply/battery/voltage_now"))));
            String readLine = bufferedReader.readLine();
            bufferedReader.close();
            return readLine;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getIsBatteryValue() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("/sys/class/lg_power_gpio/lg_power"))));
            String readLine = bufferedReader.readLine();
            bufferedReader.close();
            return readLine;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
